package com.erp.gather.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.erp.gather.domain.bo.ErpProductBrandBo;
import com.erp.gather.domain.vo.ErpProductBrandVo;
import com.erp.gather.mapper.ErpProductBrandMapper;
import com.erp.gather.service.IErpProductBrandService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.oldUtils.StringUtils;
import com.ruoyi.common.core.web.oldPage.TableDataInfo;
import com.ruoyi.gather.api.domain.ErpProductBrand;
import com.sun.org.apache.regexp.internal.RE;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.*;

/**
 * 商品品牌Service业务层处理
 *
 * @author erp
 * @date 2023-03-23
 */
@RequiredArgsConstructor
@Service
public class ErpProductBrandServiceImpl implements IErpProductBrandService {

    private final ErpProductBrandMapper baseMapper;

    /**
     * 查询商品品牌
     */
    @Override
    public ErpProductBrandVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询商品品牌列表
     */
    @Override
    public TableDataInfo<ErpProductBrandVo> queryPageList(ErpProductBrandBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ErpProductBrand> lqw = buildQueryWrapper(bo);
        Page<ErpProductBrandVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        result.getRecords().forEach(vo -> {
        	Long proNumber = baseMapper.selectByBrandId(vo.getId());
        	vo.setProNumber(proNumber);
        });
        return TableDataInfo.build(result);
    }

    /**
     * 查询商品品牌列表
     */
    @Override
    public List<ErpProductBrandVo> queryList(ErpProductBrandBo bo) {
        LambdaQueryWrapper<ErpProductBrand> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ErpProductBrand> buildQueryWrapper(ErpProductBrandBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ErpProductBrand> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getBrandName()), ErpProductBrand::getBrandName, bo.getBrandName());
        lqw.eq(StringUtils.isNotBlank(bo.getRevision()), ErpProductBrand::getRevision, bo.getRevision());
        return lqw;
    }

    /**
     * 新增商品品牌
     */
    @Override
    public Boolean insertByBo(ErpProductBrandBo bo) {
        ErpProductBrand add = BeanUtil.toBean(bo, ErpProductBrand.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改商品品牌
     */
    @Override
    public Boolean updateByBo(ErpProductBrandBo bo) {
        ErpProductBrand update = BeanUtil.toBean(bo, ErpProductBrand.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ErpProductBrand entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除商品品牌
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }



    @Override
    public R<Object> selectProductBrandAllList() {
        List<ErpProductBrand> erpProductBrands = baseMapper.selectList();
        return R.ok(erpProductBrands);
    }

    @Override
    public R<Object> selectProductBrandInfo(ErpProductBrand productBrandSel) {
        ErpProductBrandBo bo = BeanUtil.copyProperties(productBrandSel, ErpProductBrandBo.class);
        ErpProductBrand productBrand = baseMapper.selectOne(buildQueryWrapper(bo));
        if (Objects.isNull(productBrand)){
            return R.fail("信息查看失败");
        }else {
            return R.ok(productBrand);
        }
    }

    @Override
    public R<Object> selectProductBrandListInfo(ErpProductBrand productBrandSel) {
        ErpProductBrandBo bo = BeanUtil.copyProperties(productBrandSel, ErpProductBrandBo.class);
        List<ErpProductBrand> productBrand = baseMapper.selectList(buildQueryWrapper(bo));
        if (Objects.isNull(productBrand)){
            return R.fail("信息查看失败");
        }else {
            return R.ok(productBrand);
        }
    }

    @Override
    public R<Object> getProductBrandById(Long brandId) {
        ErpProductBrand productBrand = baseMapper.selectById(brandId);
        if (Objects.isNull(productBrand)){
            return R.fail(null);
        }else {
            return R.ok(productBrand);
        }
    }

    /**
     * @Description: 根据品牌名称获取品牌ID，若没有，直接新增，默认需要授权
     * @Author: j.zh
     * @Date: 2024/6/12
     * @param name
     * @return
     * @Version: 4.0.0
     **/
    @Override
    public String getProductBrandById(String name){
        ErpProductBrand brand = baseMapper.selectOne(new LambdaQueryWrapper<ErpProductBrand>()
                .eq(ErpProductBrand::getBrandName, name));
        if (brand != null){
            return brand.getId().toString();
        }
        brand = new ErpProductBrand();
        brand.setBrandName(name);
        brand.setCreateBy("1");
        brand.setCreateTime(new Date());
        brand.setEmpower(1);
        baseMapper.insert(brand);
        return brand.getId().toString();
    }
}
